package com.turing.usdtpay.mvp.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseActivity;
import com.turing.usdtpay.common.StateContants;
import com.turing.usdtpay.entity.AuthData;
import com.turing.usdtpay.http.ApiUrl;
import com.turing.usdtpay.mvp.contract.AuthContract;
import com.turing.usdtpay.mvp.presenter.AuthPresenter;
import com.turing.usdtpay.mvp.view.BaseTitleView;
import com.turing.usdtpay.mvp.view.CircularProgressView;
import com.turing.usdtpay.utils.FileSizeUtils;
import com.turing.usdtpay.utils.LogUtils;
import com.turing.usdtpay.utils.PhotoUtils;
import com.turing.usdtpay.utils.ToastUtils;

import java.io.File;

/**
 * @作者：JAY
 * @创建时间：2019/10/16 15:56
 * @描述：
 **/
public class AuthActivity extends Activity implements View.OnClickListener , AuthContract.View {

    private Bitmap bitmap;
    private BaseTitleView mTitle;
    private Button mCommit;
    private EditText mName,mNumber;
    private Button mUpLoadFace,mUpLoadBack,mUpLoadHand;
    private ImageView mPhotoFace,mPhotoBack,mPhotoHand;
    private boolean isFace,isBack,isHand;
    private AuthContract.Present mPresent;
    private int authStatus; //(0未认证,1已认证,2待审核,3认证失败)
    private TextView mTip;
    private ProgressBar mProgress;
    private CircularProgressView mCircularProgress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth);
        initView();
        initTitle();
        setPresenter(new AuthPresenter(this));
        initData();
    }

    private void initData() {
        mPresent.getAuthInfo(this);
    }

    private void initTitle() {
        mTitle.setLeftShow(true);
        mTitle.setLeftButtonListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitle.setTitle("实名认证");
    }

    private void initView() {
        mTitle = (BaseTitleView) findViewById(R.id.title);
        mCommit = (Button) findViewById(R.id.btn_commit);
        mName = (EditText) findViewById(R.id.et_name);
        mNumber = (EditText) findViewById(R.id.et_number);
        mUpLoadFace = (Button) findViewById(R.id.btn_upload_face);
        mUpLoadBack = (Button) findViewById(R.id.btn_upload_back);
        mUpLoadHand = (Button) findViewById(R.id.btn_upload_hand);
        mPhotoFace = (ImageView) findViewById(R.id.iv_photo_face);
        mPhotoHand = (ImageView) findViewById(R.id.iv_photo_hand);
        mPhotoBack = (ImageView) findViewById(R.id.iv_photo_back);
        mProgress = (ProgressBar) findViewById(R.id.progress);
        mCircularProgress = (CircularProgressView) findViewById(R.id.circular_progress);
        mTip = (TextView) findViewById(R.id.tv_tip);
        mUpLoadFace.setOnClickListener(this);
        mUpLoadBack.setOnClickListener(this);
        mUpLoadHand.setOnClickListener(this);
        mPhotoFace.setOnClickListener(this);
        mPhotoBack.setOnClickListener(this);
        mPhotoHand.setOnClickListener(this);
        mCommit.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == mUpLoadFace.getId()){
            PhotoUtils.selectPic(this,1);
        }else if (v.getId() == mUpLoadBack.getId()){
            PhotoUtils.selectPic(this,2);
        }else if (v.getId() == mUpLoadHand.getId()){
            PhotoUtils.selectPic(this,3);
        }else if (v.getId() == mPhotoFace.getId()){
            PhotoUtils.selectPic(this,1);
        }else if (v.getId() == mPhotoBack.getId()){
            PhotoUtils.selectPic(this,2);
        }else if (v.getId() == mPhotoHand.getId()){
            PhotoUtils.selectPic(this,3);
        }else if (v.getId() == mCommit.getId()){
            if (isFace&&isBack&&isHand){
               if (!TextUtils.isEmpty(mName.getText().toString())&&!TextUtils.isEmpty(mNumber.getText().toString())){
                   mProgress.setVisibility(View.VISIBLE);
                   mPresent.saveAuthInfo(this,mName.getText().toString(),mNumber.getText().toString());
               }else {
                   ToastUtils.showNormolToast(AuthActivity.this,"请填写姓名或身份证号码");
               }
            }else {
                ToastUtils.showNormolToast(AuthActivity.this,"请上传全部有效图片");
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.d("onActivityResult --> request :"+StateContants.PHOTO_REQUEST);
        if (StateContants.PHOTO_REQUEST == 1){
            File file = showPhoto(mUpLoadFace, mPhotoFace, requestCode, resultCode, data);
            upLoadImg(3,file);
        }else if (StateContants.PHOTO_REQUEST == 2){
            File file = showPhoto(mUpLoadBack, mPhotoBack, requestCode, resultCode, data);
            upLoadImg(4,file);
        }else if (StateContants.PHOTO_REQUEST == 3){
            File file = showPhoto(mUpLoadHand, mPhotoHand, requestCode, resultCode, data);
            upLoadImg(5,file);
        }

    }

    private void upLoadImg(int type, File file) {
        try {
            double size = FileSizeUtils.FormetFileSize(FileSizeUtils.getFileSize(file), FileSizeUtils.SIZETYPE_MB);
            LogUtils.d("图片大小 -->"+size);
            if (size > 10){
                ToastUtils.showNormolToast(AuthActivity.this,"图片不能超过10M，请重新选择");
                return;
            }
            LogUtils.d("file -->"+file.getPath());
            mPresent.uploadImg(this,"",type,file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private File showPhoto(Button mUpLoad, ImageView mPhoto,int requestCode, int resultCode,Intent data) {
        if (requestCode == PhotoUtils.PHOTO_CAMERA && resultCode == RESULT_OK) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2;
            bitmap = BitmapFactory.decodeFile(PhotoUtils.PhotoPath,options);
            if (bitmap != null) {
                mPhoto.setVisibility(View.VISIBLE);
                mUpLoad.setVisibility(View.GONE);
                mPhoto.setImageBitmap(bitmap);
                mCircularProgress.setVisibility(View.VISIBLE);
            }
            return new File(PhotoUtils.PhotoPath);
        } else if (requestCode == PhotoUtils.PHOTO_COLUMN && resultCode == RESULT_OK) {
            if (data != null){
                mPhoto.setVisibility(View.VISIBLE);
                mUpLoad.setVisibility(View.GONE);
                Uri uri = data.getData();
                Picasso.get().load(uri).into(mPhoto);
                mCircularProgress.setVisibility(View.VISIBLE);
                String path = PhotoUtils.getRealPathFromUri(AuthActivity.this, uri);    //获取文件真实地址
                LogUtils.d("path ->"+path);
                return new File(path);
            }
        }
        return null;
    }

    @Override
    public void getSuccess(AuthData data) {
        authStatus = data.getCheck_status();
        if (authStatus == 0){
            mTip.setVisibility(View.GONE);
            mCommit.setText("确认提交");
            mCommit.setBackgroundResource(R.drawable.btn_blue_bg);
            mCommit.setEnabled(true);
        }else if (authStatus == 1){
            mTip.setVisibility(View.GONE);
            mCommit.setText("已实名认证");
            mCommit.setBackgroundResource(R.drawable.btn_deep_gray_bg);
            mName.setEnabled(false);
            mNumber.setEnabled(false);
            mCommit.setEnabled(false);
        }else if (authStatus == 2){
            mTip.setVisibility(View.GONE);
            mCommit.setText("待审核");
            mCommit.setBackgroundResource(R.drawable.btn_deep_gray_bg);
            mCommit.setEnabled(false);
            mName.setEnabled(false);
            mNumber.setEnabled(false);
        }else if (authStatus == 3){
            mTip.setVisibility(View.VISIBLE);
            mCommit.setText("确认提交");
            mCommit.setBackgroundResource(R.drawable.btn_blue_bg);
            mCommit.setEnabled(true);
        }
        mName.setText(data.getName());
        mNumber.setText(data.getCheck_num());
        if (!TextUtils.isEmpty(data.getFront_url())){
            String replaceAll = data.getFront_url().replaceAll("\\\\", "");
            mPhotoFace.setVisibility(View.VISIBLE);
            mUpLoadFace.setVisibility(View.GONE);
            Picasso.get().load(ApiUrl.API_BASE_URL+replaceAll).into(mPhotoFace);
            mPhotoFace.setEnabled(false);
        }
        if (!TextUtils.isEmpty(data.getBack_url())){
            String replaceAll = data.getBack_url().replaceAll("\\\\", "");
            mPhotoBack.setVisibility(View.VISIBLE);
            mUpLoadBack.setVisibility(View.GONE);
            Picasso.get().load(ApiUrl.API_BASE_URL+replaceAll).into(mPhotoBack);
            mPhotoBack.setEnabled(false);
        }
        if (!TextUtils.isEmpty(data.getHand_url())){
            mPhotoHand.setVisibility(View.VISIBLE);
            mUpLoadHand.setVisibility(View.GONE);
            String replaceAll = data.getHand_url().replaceAll("\\\\", "");
            Picasso.get().load(ApiUrl.API_BASE_URL+replaceAll).into(mPhotoHand);
            mPhotoHand.setEnabled(false);
        }
    }

    @Override
    public void uploadSuccess(int type) {
        mCircularProgress.setVisibility(View.GONE);
        mCircularProgress.setProgress(0);
        ToastUtils.showNormolToast(this,"上传成功");
        if (type == 3){
            isFace = true;
        }
        if (type == 4){
            isBack = true;
        }
        if (type == 5){
            isHand = true;
        }
    }

    @Override
    public void saveSuccess() {
        mProgress.setVisibility(View.GONE);
        mCommit.setText("待审核");
        mCommit.setBackgroundResource(R.drawable.btn_deep_gray_bg);
        mCommit.setEnabled(false);
    }

    @Override
    public void onFail(String error) {
        mProgress.setVisibility(View.GONE);
        mCircularProgress.setVisibility(View.GONE);
        ToastUtils.showNormolToast(this,error);
    }

    @Override
    public void onProgress(int progress) {
        mCircularProgress.setProgress(progress);
    }

    @Override
    public void setPresenter(AuthContract.Present presenter) {
        mPresent = presenter;
    }
}
